#!/bin/bash
echo "root:$PASSWD" | chpasswd
if [ "$USER" != "root" ]; then
    if [ id -u "$USER" >/dev/null 2>&1 ]; then
        echo "User $USER already exists"
    else
        groupadd -g $GID $USER
        useradd $USER --shell /bin/bash --create-home -u $UID -g $GID -c "$USER"
        usermod -aG adm,audio,pulse-access,ssl-cert,video,voice,tty,sudo,plugdev,lpadmin,cdrom $USER
        echo "$USER:$PASSWD" | chpasswd
        echo "$USER ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/$USER
        chmod a+w /home/$USER # for NX to create ~/.nx
    fi
fi
export PASSWD=''
service dbus start

if [ "${REMOTE_DESKTOP}" = "nomachine" ]; then
    echo "start nomachine"
    service ssh start
    /etc/NX/nxserver --virtualgl yes # globally enable virtualGL
    /etc/NX/nxserver --startup
elif [ "${REMOTE_DESKTOP}" = "vnc" ]; then
    echo "start vnc"
    /usr/bin/supervisord -c /scripts/supervisord.conf
else
    echo  "unsupported remote desktop: $REMOTE_DESKTOP"
fi

# execute custom commands
exec "$@"